package com.ibeeking.found.upms.cp.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.cp.rest.service.ICpTpPermissionGroupService;
import com.ibeeking.found.upms.cp.rest.service.ICpTpPermissionService;
import com.ibeeking.found.upms.service.common.bo.CpPermissionBO;
import com.ibeeking.found.upms.service.common.param.CpTpPermissionGroupParam;
import com.ibeeking.found.upms.service.common.vo.CpTpPermissionGroupVO;
import com.ibeeking.found.upms.service.common.vo.PermissionVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName PermissionController控制器
 * @Description
 * @Author ibeeking
 * @Date 2020-12-01 18:15
 **/
@RestController
@RequestMapping("/cp/cpTpPermission/v1")
public class CpTpPermissionController {

    @Resource
    private ICpTpPermissionGroupService cpTpPermissionServiceGroupImpl;

    @Resource
    private ICpTpPermissionService cpTpPermissionServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询分组列表")
    @PostMapping(value = "/queryAllEffectiveGroup")
    public List<CpTpPermissionGroupVO> queryAllEffectiveGroup() {
        return cpTpPermissionServiceGroupImpl.queryEffective();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询分组列表")
    @PostMapping(value = "/queryEffectiveGroupWithPermission")
    public List<CpTpPermissionGroupVO> queryEffectiveGroupWithPermission(@LoginUser(isFull = true) CurrentUser currentUser, HttpServletRequest request) {
        String jti = request.getHeader(HeaderNounConstant.JWT_JTI);
        return cpTpPermissionServiceGroupImpl.queryEffectiveGroupWithPermission(currentUser,jti);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增分组")
    @PostMapping(value = "/addTpPermissionGroup")
    public Boolean addGroup(@RequestBody @Validated(BaseValidationGroup.Insert.class) CpTpPermissionGroupParam param) {
        return cpTpPermissionServiceGroupImpl.add(param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改分组")
    @PostMapping(value = "/modifyTpPermissionGroup")
    public Boolean modifyGroup(@RequestBody @Validated(BaseValidationGroup.Update.class) CpTpPermissionGroupParam param) {
        return cpTpPermissionServiceGroupImpl.modify(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询分组")
    @GetMapping(value = "/viewTpPermissionGroup/{id}")
    public CpTpPermissionGroupVO viewGroup(@PathVariable("id") Long id) {
        return cpTpPermissionServiceGroupImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询分组列表")
    @PostMapping(value = "/queryAllEffective")
    public List<CpPermissionBO> queryAllEffectiveTpPermission() {
        return cpTpPermissionServiceImpl.queryEffective();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询")
    @GetMapping(value = "/viewTpPermission/{id}")
    public PermissionVO viewTpPermission(@PathVariable("id") Long id) {
        return cpTpPermissionServiceImpl.view(id);
    }

}
